package com.yileiqian.alg;

import java.util.Random;

public class MatrixRotate {

	public static void rotate(int[][] matrix) {
		int n = matrix.length;
		if (n < 1) {
			return;
		}

		System.out.println("Start Processing: " + n);

		for (int j = 0; j < n / 2; j++) {
			for (int i = 0; i < n - 2 * j - 1; i++) {
				int temp = matrix[i + j][j];
				matrix[i + j][j] = matrix[n - j - 1][i + j];
				matrix[n - j - 1][i + j] = matrix[n - j - i - 1][n - 1 - j];
				matrix[n - j - i - 1][n - 1 - j] = matrix[j][n - j - i - 1];
				matrix[j][n - j - i - 1] = temp;
			}
		}

	}

	private static void printMatrix(int[][] matrix) {
		for (int x = 0; x < matrix.length; x++) {
			for (int y = 0; y < matrix[x].length; y++) {
				System.out.print(matrix[x][y] + " ");
			}
			System.out.println();
		}
	}

	public static void main(String args[]) {
		Random r = new Random();
		int[][] matrix = new int[6][6];
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				matrix[i][j] = r.nextInt(10);
			}
		}
		printMatrix(matrix);

		System.out.println();

		rotate(matrix);

		printMatrix(matrix);
	}
}
